var express = require("express");
var router = express.Router();
var db = require("../model/index");
/* 查询所有商品 */
router.get("/goodst/goodstt", function (req, res, next) {
    let mysql = `SELECT * FROM goodst`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询角色信息失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询角色信息成功",
            data: data,
        });
    });
});

/* 查询所有数据 */
router.get("/goodst/goodsttt", function (req, res, next) {
    let mysql = `SELECT * FROM goodstt`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询角色信息失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询角色信息成功",
            data: data,
        });
    });
});

/* 查询所有数据 */
router.get("/goodst/jysja", function (req, res, next) {
    let mysql = `SELECT * FROM jysj`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询角色信息失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询角色信息成功",
            data: data,
        });
    });
});

/* 查询所有数据 */
router.get("/goodst/jysjb", function (req, res, next) {
    let mysql = `SELECT * FROM jysj2`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询角色信息失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询角色信息成功",
            data: data,
        });
    });
});

/* 查询所有数据 */
router.get("/goodst/jysjc", function (req, res, next) {
    let mysql = `SELECT * FROM kh`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询角色信息失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询角色信息成功",
            data: data,
        });
    });
});

/* 查询所有商品分类 */
router.get("/goodst/getclass", function (req, res, next) {
    let mysql = `SELECT * FROM goods_class limit 0,6`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询角色信息失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询角色信息成功",
            data: data,
        });
    });
});

/* 关键字查询商品 */
router.get("/goodst/goodkey", function (req, res, next) {
    let mysql = '';
    if (req.query.classid > 0) {
        mysql = `SELECT * FROM goods WHERE NAME LIKE '%${req.query.keyWord}%' AND classid=${req.query.classid} GROUP BY NAME`;
    } else {
        mysql = `SELECT * FROM goods WHERE NAME LIKE '%${req.query.keyWord}%' GROUP BY NAME`;
    }
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询角色信息失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询角色信息成功",
            data: data,
        });
    });
});

module.exports = router;